package com.jml.juc.线程池;

import java.util.concurrent.*;

public class CallableDemo {

    public static void main(String[] args) throws Exception {
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        Future<String> future= newCachedThreadPool.submit(new TaskCallable());
        String result = future.get();
        System.out.println(result);
        newCachedThreadPool.shutdown();
        //也可以直接调用call方法获取返回值
        String call = new TaskCallable().call();
        System.out.println(call);

    }

    static class TaskCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("running wait 5s  begging");
            Thread.sleep(5000);
            System.out.println("running wait 5s  ending");
            return "I am return result";
        }
    }

    static class TaskRunable implements Runnable  {
        @Override
        public void run() {
            System.out.println("running wait 5s  begging");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("running wait 5s  ending");
        }
    }
}
